package com.kekezu.kppw.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalDb;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.kekezu.kppw.bean.UserBean;
import com.kekezu.kppw.utils.ConfigInc;
import com.kekezu.kppw.utils.HttpUtils;

@SuppressWarnings("deprecation")
public class IndustryData {
	static Intent intent;
	static FinalDb db;

	/**
	 * 热门分类
	 * 
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getHotCate(Context context) {
		ArrayList<HashMap<String, Object>> hotCateList = new ArrayList<HashMap<String, Object>>();
		String urlString = ConfigInc.getServiceAdress(context) + "hotCate";
		HttpUtils httpUtils = HttpUtils.getInstance();

		// db = ConfigInc.getCreateDB(context);
		// List<UserBean> users = db.findAll(UserBean.class);

		try {
			String strJson = httpUtils.httpGet(urlString, context);

			JSONObject jsonObject = new JSONObject(strJson);
			String resString = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				JSONArray jsonArray = jsonObject.getJSONArray("data");

				for (int i = 0; i < jsonArray.length(); i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					JSONObject itemJsonObject = jsonArray.optJSONObject(i);
					map.put("id", itemJsonObject.getString("id"));
					map.put("name", itemJsonObject.getString("name"));
					map.put("pid", itemJsonObject.getString("pid"));
					// map.put("type", itemJsonObject.getString("type"));
					map.put("sort", itemJsonObject.getString("sort"));
					map.put("choose_num",
							itemJsonObject.getString("choose_num"));
					hotCateList.add(map);
				}
			} else {

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return hotCateList;
	}

	/**
	 * 大分类
	 * 
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getBigCate(Context context) {
		ArrayList<HashMap<String, Object>> allCateList = new ArrayList<HashMap<String, Object>>();
		String urlString = ConfigInc.getServiceAdress(context) + "taskCate";
		HttpUtils httpUtils = HttpUtils.getInstance();

		// db = ConfigInc.getCreateDB(context);
		// List<UserBean> users = db.findAll(UserBean.class);

		try {
			String strJson = httpUtils.httpGet(urlString, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resString = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				JSONArray jsonArray = jsonObject.getJSONArray("data");

				for (int i = 0; i < jsonArray.length(); i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					JSONObject itemJsonObject = jsonArray.optJSONObject(i);
					map.put("id", itemJsonObject.getString("id"));
					map.put("name", itemJsonObject.getString("name"));
					// map.put("type", itemJsonObject.getString("type"));
					map.put("sort", itemJsonObject.getString("sort"));
					map.put("choose_num",
							itemJsonObject.getString("choose_num"));
					map.put("children_task",
							itemJsonObject.getString("children_task")
									.toString());
					allCateList.add(map);
				}
			} else {

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return allCateList;
	}

	/**
	 * 小分类
	 * 
	 * @param str
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getCate(String str) {
		ArrayList<HashMap<String, Object>> allCateList = new ArrayList<HashMap<String, Object>>();
		try {
			JSONArray jsonArray = new JSONArray(str);

			// Log.e("jsonArray", "" + jsonArray);

			for (int i = 0; i < jsonArray.length(); i++) {
				HashMap<String, Object> map = new HashMap<String, Object>();
				JSONObject itemJsonObject = jsonArray.optJSONObject(i);

				// Log.e("itemJsonObject", "" + itemJsonObject);

				map.put("id", itemJsonObject.getString("id"));
				map.put("pid", itemJsonObject.getString("pid"));
				map.put("name", itemJsonObject.getString("name"));
				// map.put("type", itemJsonObject.getString("type"));
				map.put("sort", itemJsonObject.getString("sort"));
				map.put("choose_num", itemJsonObject.getString("choose_num"));

				allCateList.add(map);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return allCateList;
	}

	/**
	 * 一级分类信息
	 * 
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getSkill(Context context) {
		ArrayList<HashMap<String, Object>> allCateList = new ArrayList<HashMap<String, Object>>();
		String urlString = ConfigInc.getServiceAdress(context) + "user/skill";
		HttpUtils httpUtils = HttpUtils.getInstance();

		try {
			String strJson = httpUtils.httpGet(urlString, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resString = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				JSONArray jsonArray = jsonObject.getJSONArray("data");

				for (int i = 0; i < jsonArray.length(); i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					JSONObject itemJsonObject = jsonArray.optJSONObject(i);

					map.put("id", itemJsonObject.getString("id"));
					map.put("pid", itemJsonObject.getString("pid"));
					map.put("name", itemJsonObject.getString("name"));
					// map.put("type", itemJsonObject.getString("type"));
					map.put("sort", itemJsonObject.getString("sort"));
					map.put("pic", itemJsonObject.getString("pic"));
					map.put("choose_num",
							itemJsonObject.getString("choose_num"));

					allCateList.add(map);
				}
			} else {

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return allCateList;
	}

	/**
	 * 一级分类下的子分类信息
	 * 
	 * @param strId
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> secondSkill(String strId,
			Context context) {
		ArrayList<HashMap<String, Object>> allCateList = new ArrayList<HashMap<String, Object>>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/secondSkill?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		Map<String, String> params = new HashMap<String, String>();
		params.put("id", strId);

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		if (users.size() > 0) {
			params.put("token", users.get(0).getToken());
		}

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resString = jsonObject.getString("code");

			// Log.e("jsonObject", "" + jsonObject);

			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				JSONArray jsonArray = jsonObject.getJSONArray("data");

				for (int i = 0; i < jsonArray.length(); i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					JSONObject itemJsonObject = jsonArray.optJSONObject(i);

					map.put("id", itemJsonObject.getString("id"));
					map.put("pid", itemJsonObject.getString("pid"));
					map.put("name", itemJsonObject.getString("name"));
					// map.put("type", itemJsonObject.getString("type"));
					map.put("sort", itemJsonObject.getString("sort"));
					map.put("pic", itemJsonObject.getString("pic"));
					map.put("choose_num",
							itemJsonObject.getString("choose_num"));
					if (users.size() > 0) {
						map.put("isChecked",
								itemJsonObject.getString("isChecked"));
					}

					allCateList.add(map);
				}
			} else {

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return allCateList;
	}

	/**
	 * 添加、修改、取消个人标签
	 * 
	 * @param strId
	 * @param strCheck
	 * @param context
	 * @return
	 */
	public static String[] skillSave(String strId, String strCheck,
			Context context) {
		String resString[] = new String[3];
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/skillSave";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("token", users.get(0).getToken()));

		Log.e("strCheck", strCheck);
		Log.e("strId", strId);

		if (strCheck.equals("1")) {
			params.add(new BasicNameValuePair("cancel_id", strId));
		} else {
			params.add(new BasicNameValuePair("id", strId));
		}

		try {
			String strJson = httpUtils.executeRequest(urlString, params);
			JSONObject jsonObject = new JSONObject(strJson);

			Log.e("strJson", strJson);

			resString[0] = jsonObject.getString("code");
			resString[1] = jsonObject.getString("message");
			resString[2] = jsonObject.getString("data");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resString;
	}

	/**
	 * 标签一级
	 * 
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, String>> getAllSkill(
			String strShopid, Context context) {
		ArrayList<HashMap<String, String>> allCateList = new ArrayList<HashMap<String, String>>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/getAllSkill?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("token", users.get(0).getToken());
		params.put("shop_id", strShopid);
		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resString = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resString.equals("1000")) {
				JSONArray jsonArray = jsonObject.getJSONArray("data");

				for (int i = 0; i < jsonArray.length(); i++) {
					HashMap<String, String> map = new HashMap<String, String>();
					JSONObject itemJsonObject = jsonArray.optJSONObject(i);
					map.put("id", itemJsonObject.getString("id"));
					map.put("name", itemJsonObject.getString("name"));
					map.put("second_cate",
							itemJsonObject.getString("second_cate"));
					allCateList.add(map);
				}
			} else {

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return allCateList;
	}

	/**
	 * 标签二级
	 * 
	 * @param str
	 * @return
	 */
	public static ArrayList<HashMap<String, String>> getAllSkill(String str) {
		ArrayList<HashMap<String, String>> allCateList = new ArrayList<HashMap<String, String>>();
		try {
			JSONArray jsonArray = new JSONArray(str);
			for (int i = 0; i < jsonArray.length(); i++) {
				HashMap<String, String> map = new HashMap<String, String>();
				JSONObject itemJsonObject = jsonArray.optJSONObject(i);
				map.put("id", itemJsonObject.getString("id"));
				map.put("pid", itemJsonObject.getString("pid"));
				map.put("name", itemJsonObject.getString("name"));
				map.put("isChecked", itemJsonObject.getString("isChecked"));// 是否设为标签
																			// 1:是
																			// 0:否
				allCateList.add(map);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return allCateList;
	}

	public static HashMap<String, String> getQCode(String str) {
		HashMap<String, String> map = new HashMap<String, String>();
		try {
			JSONObject jsonObject = new JSONObject(str);
			if (!jsonObject.isNull("domain")) {
				map.put("domain", jsonObject.getString("domain"));
				map.put("shop_id", jsonObject.getString("shop_id"));
				map.put("is_open", jsonObject.getString("is_open"));
			}

		} catch (JSONException e) {
			e.printStackTrace();
			return map;
		}

		return map;
	}

}
